package com.gykj.rtm.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.gykj.admin.common.entity.Resources;
import com.gykj.admin.common.resp.RespResultJson;
import com.gykj.rtm.facade.admin.ResourcesFacade;

/**
 * Created by rohto.li on 2017/11/03.
 */
@Api(value = "资源操作接口")
@RestController
@RequestMapping("/resources")
public class ResourcesControllerFacade {

	@Autowired
	ResourcesFacade resourcesFacade;

	@ApiOperation(value = "检索资源" ,  notes="检索资源，支持条件模糊查询")
    @RequestMapping(value = "/getAllResourcesList", method = RequestMethod.POST)
    public RespResultJson getAllResourcesList(
    		@RequestBody Resources resources,
            @RequestParam(value="currentPage",required = false, defaultValue = "1") int currentPage,
            @RequestParam(value="pageSize",required = false, defaultValue = "20") int pageSize,
            @RequestParam(value="sortColumn",required = false, defaultValue = "1") int sortColumn,
            @RequestParam(value="sortDesc",required = false, defaultValue = "1") int sortDesc
    		)
    		{
    	RespResultJson result = new RespResultJson();
    	Map<String,Object> innerMap = new HashMap<>();
    	try {
    		PageInfo<Resources> pageInfo = resourcesFacade.getAllResourcesList(resources, currentPage, pageSize, sortColumn, sortDesc);
    		innerMap.put("rows", pageInfo.getList());
    		innerMap.put("total", pageInfo.getTotal());
    		
    		result.setStatus(true);
    		result.setResult(innerMap);
    	} catch(Exception e) {
        	result.setStatus(false);
        	result.setMessage(e.getMessage());
    	}
        return result;
    }
	
	@ApiOperation(value = "检索资源" ,  notes="通过资源ID来检索资源")
    @RequestMapping(value = "/getResourcesById", method = RequestMethod.GET)
    public RespResultJson getResourcesById(@RequestParam(value = "rid",required = false) Integer rid) {
    	RespResultJson result = new RespResultJson();
    	Map<String,Object> map = new HashMap<>();
    	try{
    		List<Resources> list = resourcesFacade.getResourcesById(rid);
    		map.put("rows", list);
    		map.put("total", 1);
    		
        	result.setStatus(true);
        	result.setResult(map);
    	}catch(Exception e) {
        	result.setStatus(false);
    		result.setMessage(e.getMessage());
    	}
        return result;
    }
    
    
	@ApiOperation(value = "加载资源菜单", notes = "加载资源菜单")
	@ApiImplicitParam(name = "uid", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @RequestMapping(value = "/loadMenu/{uid}",method=RequestMethod.GET)
    public List<Resources> loadMenu(@PathVariable(value="uid",required=false) Integer uid){
        return resourcesFacade.loadMenu(uid);
    }

	@ApiOperation(value = "添加资源", notes = "添加资源")
    @CacheEvict(cacheNames="resources", allEntries=true)
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public RespResultJson addResources(@RequestBody Resources resources) {
		RespResultJson result = new RespResultJson();
		if (StringUtils.equals("success", resourcesFacade.add(resources))) {
			result.setStatus(true);
		} else {
			result.setStatus(false);
		}
    	return result;
    }
	
	@ApiOperation(value = "删除资源", notes = "通过ID删除资源")
	@ApiImplicitParam(name = "ID", value = "资源ID", required = true, dataType = "Integer", paramType = "path")
    @CacheEvict(cacheNames="resources", allEntries=true)
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public RespResultJson delResources(@RequestParam(value="id",required=false) Integer id) {
		RespResultJson result = new RespResultJson();
		if (StringUtils.equals("success", resourcesFacade.delete(id))) {
			result.setStatus(true);
		} else {
			result.setStatus(false);
		}
    	return result;
    }
}
